package com.ikas.ai.server.module.alarm.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.consts.Consts;
import com.ikas.ai.server.module.alarm.dao.UnitDeteriorationAlarmDAO;
import com.ikas.ai.server.module.alarm.enums.SolvedEnum;
import com.ikas.ai.server.module.alarm.model.UnitDeteriorationAlarm;
import com.ikas.ai.server.module.alarm.model.enums.AlarmType;
import com.ikas.ai.server.module.condition.dao.WorkConditionFlowDao;
import com.ikas.ai.server.module.condition.model.WorkConditionFlow;
import com.ikas.ai.server.module.instance.dorisDao.DorisWorkFlowInstanceDao;
import com.ikas.ai.server.module.instance.dorisDao.DorisWorkInstanceStepDetailDao;
import com.ikas.ai.server.module.instance.enums.WorkInstanceStepDetailStateEnum;
import com.ikas.ai.server.module.instance.model.DorisWorkFlowInstance;
import com.ikas.ai.server.module.instance.model.DorisWorkInstanceStepDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * 劣化预警
 *
 * @author tang.xuandong
 * @version 1.0.0
 * @date 2023/2/1 10:41
 */
@Service
@Slf4j
public class UnitDeteriorationAlarmService extends ServiceImpl<UnitDeteriorationAlarmDAO, UnitDeteriorationAlarm> {

    //    @Autowired
//    private WorkFlowInstanceDao workFlowInstanceDao;
    @Autowired
    private DorisWorkFlowInstanceDao dorisWorkFlowInstanceDao;
    @Autowired
    private DorisWorkInstanceStepDetailDao dorisWorkInstanceStepDetailDao;
    @Autowired
    private WorkConditionFlowDao workConditionFlowDao;
    @Autowired
    private AlarmCacheService alarmCacheService;


    @Transactional(rollbackFor = Exception.class)
    public void add(DorisWorkInstanceStepDetail workInstanceStepDetail) {
        log.info("start save unit-alarm:{}", JSON.toJSONString(workInstanceStepDetail));
        UnitDeteriorationAlarm unitDeteriorationAlarm = new UnitDeteriorationAlarm();
        DorisWorkFlowInstance workFlowInstance = dorisWorkFlowInstanceDao.selectById(workInstanceStepDetail.getWfInstanceId());
        if (Objects.nonNull(workFlowInstance)) {
            unitDeteriorationAlarm.setMachineNo(workFlowInstance.getMachineNo());
            unitDeteriorationAlarm.setWcBatch(workFlowInstance.getBatch());
        }
        Long wfId = workInstanceStepDetail.getWfId();
        WorkConditionFlow workConditionFlow = workConditionFlowDao.selectById(wfId);
        unitDeteriorationAlarm.setWcDesc(workConditionFlow.getWfDesc());
        unitDeteriorationAlarm.setStepName(workInstanceStepDetail.getStepName());
        unitDeteriorationAlarm.setMeteCodeDesc(workInstanceStepDetail.getMeteName());
        unitDeteriorationAlarm.setMeteCode(workInstanceStepDetail.getMeteCode());
        unitDeteriorationAlarm.setExceptionResponseCost(workInstanceStepDetail.getCostTime());
        unitDeteriorationAlarm.setExceptionMsg(Consts.EXCEPTION_MSG);
        unitDeteriorationAlarm.setCreator(1L);
        unitDeteriorationAlarm.setCreateTime(new Date());
        unitDeteriorationAlarm.setUpdater(1L);
        unitDeteriorationAlarm.setUpdateTime(new Date());
        unitDeteriorationAlarm.setSolved(SolvedEnum.UN_SOLVED.getSolved());
        save(unitDeteriorationAlarm);


        log.info("修改状态为预警状态");
        //改变该实例步骤的状态state为3预警 t_work_instance_step_detail
        DorisWorkInstanceStepDetail dorisWorkInstanceStepDetail = new DorisWorkInstanceStepDetail();
        dorisWorkInstanceStepDetail.setId(workInstanceStepDetail.getId());
        dorisWorkInstanceStepDetail.setState(WorkInstanceStepDetailStateEnum.ALARM.getCode());
        dorisWorkInstanceStepDetail.setUpdateTime(new Date());
        dorisWorkInstanceStepDetailDao.updateById(dorisWorkInstanceStepDetail);
        log.info("修改状态为预警状态完成");
        //更新redis
        int count = count(
                new LambdaQueryWrapper<UnitDeteriorationAlarm>()
                        .eq(UnitDeteriorationAlarm::getSolved, 0)
                        .eq(UnitDeteriorationAlarm::getMachineNo, unitDeteriorationAlarm.getMachineNo())
        );
        alarmCacheService.updateAlarm(AlarmType.UNIT_DETERIORATION, unitDeteriorationAlarm.getMachineNo(), count);
    }
}
